package com.youkeda.app.util;

import com.youkeda.app.model.PreSingleRedEnvelope;
import com.youkeda.app.model.PreSingleRedEnvelopeStatus;
import com.youkeda.app.model.RedEnvelope;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName RandomRedPacket
 * @Author 刘正星
 * @Date 2020/9/4 8:34
 **/
public class RandomRedPacket {
    public static List<PreSingleRedEnvelope> random(RedEnvelope redEnvelope){
        List<PreSingleRedEnvelope> preSingleRedEnvelopes = new ArrayList<>();
        int numbers = redEnvelope.getTotalAmount();
        double amount = redEnvelope.getRedEnvelopeAmount();
        double average = amount/numbers;
        double sum = 0;
        Random random = new Random();
       List<Double> doubles = random.doubles(0.10,0.99).limit(numbers-1).map(operand -> {
           DecimalFormat df = new DecimalFormat("0.00");
           String str = df.format(operand);
           return Double.parseDouble(str)*average;
       }).boxed().collect(Collectors.toList());
       sum = doubles.stream().peek(System.out::println).reduce((a,b) -> a+b).get();
       doubles.forEach(d->{
           PreSingleRedEnvelope preSingleRedEnvelope = new PreSingleRedEnvelope();
           preSingleRedEnvelope.setRedEnvelopeAmount(d);
           preSingleRedEnvelope.setRedEnvelopeId(redEnvelope.getId());
           preSingleRedEnvelope.setRedEnvelopeId(UUIDUtils.getUUID());
           preSingleRedEnvelope.setStatus(PreSingleRedEnvelopeStatus.TODO);
           preSingleRedEnvelopes.add(preSingleRedEnvelope);
       });
       PreSingleRedEnvelope preSingleRedEnvelope= new PreSingleRedEnvelope();
       preSingleRedEnvelope.setRedEnvelopeAmount(amount-sum);
        preSingleRedEnvelope.setRedEnvelopeId(redEnvelope.getId());
        preSingleRedEnvelope.setRedEnvelopeId(UUIDUtils.getUUID());
        preSingleRedEnvelope.setStatus(PreSingleRedEnvelopeStatus.TODO);
        preSingleRedEnvelopes.add(preSingleRedEnvelope);
        return preSingleRedEnvelopes;
    }
}
